#pragma once
#include "uart/ProtocolSender.h"
#include "storage/StoragePreferences.h"

const char* Save1 = "";
const char* Save2 = "";
const char* Save3 = "";
const char* Save4 = "";
const char* Save5 = "";
const char* Save6 = "";
const char* Save7 = "";
const char* Save8 = "";

float WeightKG;
float WeightLB;
float WeightOZ;
char strLB[10];
char strOZ[10];
char strKG[10];
unsigned int Batshow_count = 0;
#define CNT_LEV 10

BYTE KGorLB_state;
char KGorLB_temp[100];

bool save_one_flag = true;
bool Charge_sleep_flag = false;
BYTE UpDownCnt = 0;

void Get_Save()
{
	std::string Save1 = StoragePreferences::getString("Save1", "");
	std::string Save2 = StoragePreferences::getString("Save2", "");
	std::string Save3 = StoragePreferences::getString("Save3", "");
	std::string Save4 = StoragePreferences::getString("Save4", "");
	std::string Save5 = StoragePreferences::getString("Save5", "");
	std::string Save6 = StoragePreferences::getString("Save6", "");
	std::string Save7 = StoragePreferences::getString("Save7", "");
	std::string Save8 = StoragePreferences::getString("Save8", "");

	if(UpDownCnt==0)
	{
		mTextViewS1Ptr->setText("1.");
		mTextViewS2Ptr->setText("2.");
		mTextViewS3Ptr->setText("3.");
		mTextViewS4Ptr->setText("4.");
		mTextViewS5Ptr->setText("5.");

		mTextViewSS1Ptr->setText(Save1.c_str());
		mTextViewSS2Ptr->setText(Save2.c_str());
		mTextViewSS3Ptr->setText(Save3.c_str());
		mTextViewSS4Ptr->setText(Save4.c_str());
		mTextViewSS5Ptr->setText(Save5.c_str());
	}
	else if(UpDownCnt==1)
	{
		mTextViewS1Ptr->setText("4.");
		mTextViewS2Ptr->setText("5.");
		mTextViewS3Ptr->setText("6.");
		mTextViewS4Ptr->setText("7.");
		mTextViewS5Ptr->setText("8.");

		mTextViewSS1Ptr->setText(Save4.c_str());
		mTextViewSS2Ptr->setText(Save5.c_str());
		mTextViewSS3Ptr->setText(Save6.c_str());
		mTextViewSS4Ptr->setText(Save7.c_str());
		mTextViewSS5Ptr->setText(Save8.c_str());
	}
}

void Save_deal()
{
	std::string Save1 = StoragePreferences::getString("Save1", "");
	std::string Save2 = StoragePreferences::getString("Save2", "");
	std::string Save3 = StoragePreferences::getString("Save3", "");
	std::string Save4 = StoragePreferences::getString("Save4", "");
	std::string Save5 = StoragePreferences::getString("Save5", "");
	std::string Save6 = StoragePreferences::getString("Save6", "");
	std::string Save7 = StoragePreferences::getString("Save7", "");
	std::string Save8 = StoragePreferences::getString("Save8", "");

	StoragePreferences::putString("Save8", Save7.c_str());
	StoragePreferences::putString("Save7", Save6.c_str());
	StoragePreferences::putString("Save6", Save5.c_str());
	StoragePreferences::putString("Save5", Save4.c_str());
	StoragePreferences::putString("Save4", Save3.c_str());
	StoragePreferences::putString("Save3", Save2.c_str());
	StoragePreferences::putString("Save2", Save1.c_str());

	if(KGorLB_state)
	{
		sprintf(KGorLB_temp, "%s LB %s OZ", strLB, strOZ);
	}
	else
	{
		sprintf(KGorLB_temp, "%s KG", strKG);
	}
	StoragePreferences::putString("Save1", KGorLB_temp);

	//LOGD(" **********KGorLB_temp*********** %s !!!\n",KGorLB_temp);
}

/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	//{0,  100}, //定时器id=0, 时间间隔6秒
	//{1,  1000},
};

/**
 * 当界面构造时触发
 */
static void onUI_init(){
    //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");
	mPointer1Ptr->setTargetAngle(0);
	//mTextView15Ptr->setText(5);
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {

}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {
	if(Charge_sleep_flag)
		mWindow5Ptr->showWnd();
	else
		mWindow5Ptr->hideWnd();

///////////////////电量状态显示///////////////////
	if(data.BatCharge==0)
	{
		if(data.BAT_level)
		{
			mTextView15Ptr->setVisible(true);
			mTextView16Ptr->setVisible(false);
			mTextViewLowPowerPtr->setVisible(false);
			mTextView15Ptr->setText(data.BAT_level);
		}
		else
		{
			mTextView15Ptr->setVisible(false);
			mTextView16Ptr->setVisible(false);
			mTextViewLowPowerPtr->setVisible(true);
		}
	}
	else if(data.BatCharge==1)
	{
		mTextView15Ptr->setVisible(false);
		mTextView16Ptr->setVisible(true);
		mTextViewLowPowerPtr->setVisible(false);
		Batshow_count++;
		if(Batshow_count==1*CNT_LEV)
		{
			mTextView16Ptr->setText(1);
			mTextView10Ptr->setText(1);
		}
		else if(Batshow_count==2*CNT_LEV)
		{
			mTextView16Ptr->setText(2);
			mTextView10Ptr->setText(2);
		}
		else if(Batshow_count==3*CNT_LEV)
		{
			mTextView16Ptr->setText(3);
			mTextView10Ptr->setText(3);
		}
		else if(Batshow_count==4*CNT_LEV)
		{
			mTextView16Ptr->setText(4);
			mTextView10Ptr->setText(4);
		}
		else if(Batshow_count==5*CNT_LEV)
		{
			mTextView16Ptr->setText(5);
			mTextView10Ptr->setText(5);
		}
		else if(Batshow_count==6*CNT_LEV)
			Batshow_count = 0;
	}
	else if(data.BatCharge==2)
	{
		mTextView15Ptr->setVisible(true);
		mTextView16Ptr->setVisible(false);
		mTextViewLowPowerPtr->setVisible(false);
		mTextView15Ptr->setText(5);
	}
///////////////////电量状态显示///////////////////

//////////////////////重量显示///////////////////
	WeightKG = (float(data.BAI*10000+data.SHI*1000+data.GE*100+data.D_SHI*10+data.D_GE))/100;

	if(data.SaveDeal==0)
	{
		if(data.KGorLB==0x04)
		{
			KGorLB_state = 1;

			mWindow1Ptr->hideWnd();
			mWindow2Ptr->showWnd();

			WeightLB = WeightKG*35.274/16;
			WeightOZ = WeightKG*35.274 - ((BYTE) WeightLB)*16;

			sprintf(strLB, "%3d", (BYTE)WeightLB);
			mTextView5Ptr->setText(strLB);
			mTextView22Ptr->setText((BYTE)WeightLB/100);
			mTextView21Ptr->setText((BYTE)WeightLB%100/10);
			mTextView20Ptr->setText((BYTE)WeightLB%100%10);
			sprintf(strOZ, "%2d", (BYTE)WeightOZ);
			mTextView6Ptr->setText(strOZ);
			mTextView19Ptr->setText((BYTE)WeightOZ/10);
			mTextView18Ptr->setText((BYTE)WeightOZ%10);
			mPointer2Ptr->setTargetAngle(WeightLB*297/132);

			LOGD("strLB = %s ,strOZ = %s \n", strLB,strOZ);
		}
		else
		{
			KGorLB_state = 0;

			mWindow2Ptr->hideWnd();
			mWindow1Ptr->showWnd();

			//LOGD("onProtocolDataUpdate = %.2f \n", WeightKG);
			sprintf(strKG, "%2.2f", WeightKG);
			mTextView3Ptr->setText(strKG);
			mTextView14Ptr->setText((BYTE)WeightKG/10);
			mTextView13Ptr->setText((BYTE)WeightKG%10);

			mTextView12Ptr->setText(((UINT16)(WeightKG*100))/10%10);
			mTextView11Ptr->setText(((UINT16)(WeightKG*100))%10);
			mPointer1Ptr->setTargetAngle(WeightKG*300/60);
		}
	}
//////////////////////重量显示///////////////////


	if(data.QuPi)
	{
		mTextView1Ptr->setVisible(true);
		save_one_flag = true;
	}
	else
		mTextView1Ptr->setVisible(false);

/////////////////标定显示////////////////////
	if(data.BiaoDing)
	{
		mWindow3Ptr->showWnd();
		mTextView9Ptr->setText(data.BiaoDing);
		int neima = data.BAI*10000+data.SHI*1000+data.GE*100+data.D_SHI*10+data.D_GE;
		//LOGD("onProtocolDataUpdate = %d \n", neima);
		if(data.BiaoDing==1)
		{
			mTextView23Ptr->setVisible(true);
			mTextView23Ptr->setText(neima);
		}
		else
			mTextView23Ptr->setVisible(false);
	}
	else
		mWindow3Ptr->hideWnd();
/////////////////标定显示////////////////////

/////////////////存储显示////////////////////
	if(data.SaveState)
	{
		if(data.SaveState==1)
			UpDownCnt = 0;
		else if(data.SaveState==2)//下翻
		{
			UpDownCnt = 1;
		}
		else if(data.SaveState==3)//上翻
		{
			UpDownCnt = 0;
		}

		mWindow4Ptr->showWnd();
		Get_Save();
	}
	else
		mWindow4Ptr->hideWnd();

	if(data.SaveDeal)
	{
		//LOGD("data.Hold = %.2f \n", data.Hold);
		if(data.Hold&&save_one_flag)
		{
			save_one_flag = false;
			mTextView2Ptr->setVisible(true);
			Save_deal();
		}
	}
	else
		mTextView2Ptr->setVisible(false);
/////////////////存储显示////////////////////
}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
int angle_count = 0;
static bool onUI_Timer(int id){
	switch (id) {
		case 0:
			angle_count++;
			if(angle_count>300)
				angle_count = 0;
			mPointer1Ptr->setTargetAngle(angle_count);
			break;

		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onmainActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}
